package com.example.app.personManagement;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.app.R;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class PersonActivity extends AppCompatActivity implements OnClickListener {

    private static final String TAG = "Add";
    private EditText ednumber, edname, editPhone, edage, editSalary, edjob,editMoney,editFa;
    private Spinner spdepartment, spinner_power,spinner_meno,spinner_fa;
    private RadioButton radio1, radio2;
    private Person person = null;
    PersonDAO personDAO = new PersonDAO(this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staff_management);

        if (this.getIntent() != null && this.getIntent().getExtras() != null) {
            Serializable serializable = this.getIntent().getExtras().getSerializable("Person");
            if (serializable != null) {
                person = (Person) serializable;
            }
        }
        ednumber = (EditText) findViewById(R.id.ednumber);
        edname = (EditText) findViewById(R.id.edname);
        edage = (EditText) findViewById(R.id.edage);
        editPhone = (EditText) findViewById(R.id.editPhone);
        editSalary = (EditText) findViewById(R.id.editSalary);
        edjob = (EditText) findViewById(R.id.edjob);
        spdepartment = (Spinner) findViewById(R.id.spdepartment);
        radio1 = (RadioButton) findViewById(R.id.radio0);
        radio2 = (RadioButton) findViewById(R.id.radio1);
        findViewById(R.id.buadd).setOnClickListener(this);
        findViewById(R.id.budelete).setOnClickListener(this);



        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.departmentName, android.R.layout.simple_spinner_item);
        spdepartment.setAdapter(adapter);


        if (person != null){
            findViewById(R.id.budelete).setVisibility(View.VISIBLE);
            List<String> list = Arrays.asList(getResources().getStringArray(R.array.departmentName));

            edname.setText(person.getName());
            edage.setText(String.valueOf(person.getAge()));
            spdepartment.setSelection(list.indexOf(person.getDepartment()),true);


            edjob.setText(person.getJob());
            ednumber.setText(person.getNumber());
            editSalary.setText(person.getSalary());
            editPhone.setText(person.getPhone());

            if (person.getSex().equalsIgnoreCase("男")){
                radio1.setChecked(true);
            }else {
                radio2.setChecked(true);
            }
            Button button = findViewById(R.id.buadd);
            button.setText("更新");
            setTitle("员工信息修改");
        }else {
            findViewById(R.id.budelete).setVisibility(View.GONE);
            setTitle("员工信息添加");
        }


    }

    private void delete() {
        Builder builder = new Builder(this);
        builder.setTitle("提示");
        builder.setMessage("确认删除此员工信息么？");
        builder.setPositiveButton(R.string.btnOK, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                try {
                    personDAO.delete(ednumber.getText().toString());
                    Toast.makeText(PersonActivity.this, "员工编号为" + ednumber.getText().toString() + "的员工删除成功！", Toast.LENGTH_LONG).show();
                    finish();
                } catch (Exception e) {
                    Log.i("Delete", e.getMessage());
                }
            }
        });
        builder.setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }

        });
        builder.create().show();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.budelete:
                delete();

                break;
            //增加
            case R.id.buadd:

                if (ednumber.getText().toString().equals("")) {
                    Toast.makeText(PersonActivity.this, "员工编号不能为空！", Toast.LENGTH_LONG).show();
                    return;
                }
                if (edname.getText().toString().equals("")) {
                    Toast.makeText(PersonActivity.this, "名字不能为空！", Toast.LENGTH_LONG).show();
                    return;
                }

                if (person != null) {
                    person.setName(edname.getText().toString());
                    person.setAge(Integer.valueOf(edage.getText().toString()));
                    person.setDepartment(spdepartment.getSelectedItem().toString());
                    person.setJob(edjob.getText().toString());
                    person.setNumber(ednumber.getText().toString());
                    person.setSalary(editSalary.getText().toString());
                    person.setSex(radio1.isChecked() ? "男" : "女");
                    person.setPhone(editPhone.getText().toString());


                    personDAO.update(person);
                    Toast.makeText(PersonActivity.this, "更新成功！", Toast.LENGTH_LONG).show();

                    finish();
                } else {
                    Person person1 = personDAO.find(ednumber.getText().toString());
                    if (person1 != null){
                        Toast.makeText(PersonActivity.this, "此编号的员工已存在！", Toast.LENGTH_LONG).show();
                        return;
                    }
                    person = new Person();
                    person.setName(edname.getText().toString());
                    person.setAge(Integer.valueOf(edage.getText().toString()));
                    person.setDepartment(spdepartment.getSelectedItem().toString());
                    person.setJob(edjob.getText().toString());
                    person.setNumber(ednumber.getText().toString());
                    person.setSalary(editSalary.getText().toString());
                    person.setSex(radio1.isChecked() ? "男" : "女");
                    person.setPhone(editPhone.getText().toString());


                    personDAO.add(person);
                    Toast.makeText(PersonActivity.this, "成功添加！", Toast.LENGTH_LONG).show();
                    finish();
                }

                break;
            default:
                break;
        }
    }
}